<?php

namespace app\api\controller;

use app\common\exception\BusinessException;
use think\response\Json;

class Question extends Base
{
    /**
     * 查询题目
     * @return Json
     * @throws BusinessException
     */
    public function getInfo(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '题目id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交查询
        $response = app(\app\api\logic\Question::class)->getInfo($param);

        // 返回
        return $this->response($response);
    }

    /**
     * 查询题目数量
     * @return Json
     * @throws BusinessException
     */
    public function getCount(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'course_id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'course_id.require' => '课程编号不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交查询
        $response = app(\app\api\logic\Question::class)->getCount($param);

        // 返回
        return $this->response($response);
    }

    /**
     * 搜索题目
     * @throws BusinessException
     */
    public function search(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'keyword' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'keyword.require' => '题目关键字不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 特殊验证
        if (!empty($param['keyword'])) {
            if (mb_strlen($param['keyword']) <= 2) {
                throw new BusinessException("关键词长度太短啦");
            }
        }

        // 提交查询
        $response = app(\app\api\logic\Question::class)->search($param);

        // 返回
        return $this->response($response);
    }

    /**
     * 新版本搜索
     * @return Json
     * @throws BusinessException
     */
    public function newSearch(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'keyword' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'keyword.require' => '题目关键字不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 特殊验证
        if (!empty($param['keyword'])) {
            if (mb_strlen($param['keyword']) < 5) {
                throw new BusinessException("关键词长度太短啦");
            }
        }

        // 提交查询
        $response = app(\app\api\logic\Question::class)->newSearch($param);

        // 返回
        return $this->response($response);
    }

    /**
     * 搜索题目记录
     * @throws
     */
    public function searchRecord(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 提交查询
        $response = app(\app\api\logic\Question::class)->searchRecord($param);

        // 返回
        return $this->response($response);
    }


    /**
     * 查询课程题目
     * @throws BusinessException
     */
    public function getList(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
            'type' => 'require',
            'question_type' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '课程id不得为空',
            'type.require' => '来源类型type不得为空',
            'question_type.require' => '课程题型type不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交查询
        $response = app(\app\api\logic\Question::class)->getList($param);

        // 返回
        return $this->response($response);
    }

    /**
     * 查询是否继续刷题
     * @throws BusinessException
     */
    public function isContinue(): Json
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            'id' => 'require',
            'type' => 'require',
            'question_type' => 'require',
        ];

        //错误信息
        $errMsg = [
            'id.require' => '课程id不得为空',
            'type.require' => '来源类型type不得为空',
            'question_type.require' => '课程题型type不得为空',
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        //提交查询
        $response = app(\app\api\logic\Question::class)->isContinue($param);

        //返回
        return $this->response($response);
    }

    /**
     * 重置答题记录
     * @throws BusinessException
     */
    public function clear(): Json
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            'id' => 'require',
            'type' => 'require',
            'extend_id' => 'require',
            'charge_type' => 'require',
            'question_type' => 'require',
        ];

        //错误信息
        $errMsg = [
            'id.require' => '课程id不得为空',
            'type.require' => '题目根类型不得为空',
            'extend_id.require' => '扩展id不得为空',
            'charge_type.require' => '收费类型charge_type不得为空',
            'question_type.require' => '题目类型question_type不得为空',
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        //提交操作
        app(\app\api\logic\Question::class)->clear($param);

        //返回
        return $this->response([], 1, '操作成功');
    }

    /**
     * 收藏题目
     * @throws BusinessException
     */
    public function collect(): Json
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            'id' => 'require',
            'type' => 'require',
        ];

        //错误信息
        $errMsg = [
            'id.require' => '题目id不得为空',
            'type.require' => '操作类型type不得为空',
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        //提交操作
        app(\app\api\logic\Question::class)->collect($param);

        //返回
        return $this->response([], 1, '操作成功');
    }

    /**
     * 保存答题记录
     * @throws BusinessException
     */
    public function saveAnswer(): Json
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            'course_id' => 'require',
            'answer_data' => 'require',
        ];

        //错误信息
        $errMsg = [
            'course_id.require' => '课程course_id不得为空',
            'answer_data.require' => '答题记录answer_data不得为空',
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        //特殊校验
        $checkFields = [
            'question_id',
            'question_type',
            'question_page',
            'question_answer',
            'question_correct',
            'question_main_type',
            'question_main_extend_id'
        ];
        $answer_data = $param['answer_data'];
        foreach ($checkFields as $field) {
            if (!isset($answer_data['0'][$field])) {
                throw new BusinessException("答题记录{$field}字段不得为空");
            }
        }

        //提交数据
        app(\app\api\logic\Question::class)->saveAnswer($param);

        //返回
        return $this->response([], 1, '保存成功');
    }

    /**
     * 评论题目
     * @throws BusinessException
     */
    public function comment()
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            'id' => 'require',
            'content' => 'require',
        ];

        //错误信息
        $errMsg = [
            'id.require' => '题目id不得为空',
            'content.require' => '评论内容content不得为空',
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }
    }

    /**
     * answerResult
     * @throws BusinessException
     */
    public function answerResult(): Json
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            'id' => 'require',
            'type' => 'require',
            'question_type' => 'require',
        ];

        //错误信息
        $errMsg = [
            'id.require' => '课程id不得为空',
            'type.require' => '来源类型type不得为空',
            'question_type.require' => '课程题型type不得为空',
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        //提交查询
        $response = app(\app\api\logic\Question::class)->answerResult($param);

        //返回
        return $this->response($response);
    }

    /**
     * 题目反馈
     * @throws BusinessException
     */
    public function feedback(): Json
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            //'detail' => 'require',
            'question_id' => 'require'
        ];

        //错误信息
        $errMsg = [
            //'detail.require' => '反馈内容detail不得为空',
            'question_id.require' => '题目question_id不得为空'
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        //提交数据
        app(\app\api\logic\Question::class)->feedback($param);

        //返回
        return $this->response([], 1, '反馈成功');
    }

    /**
     * Ai解析
     * @throws BusinessException
     */
    public function aiExplanation(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '题目id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交查询
        $response = app(\app\api\logic\Question::class)->aiExplanation($param);

        //返回
        return $this->response($response, 1, '已提交申请');
    }
}
